package com.example.project.controller;

import com.example.project.annotation.AdminRequired;
import com.example.project.common.Result;
import com.example.project.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 获取普通用户列表
    @AdminRequired
    @GetMapping("/users")
    public Result<?> getNormalUserList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String query,
            @RequestParam(required = false) Integer status,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("获取普通用户列表: page={}, pageSize={}, query={}, status={}", 
                page, pageSize, query, status);
        
        return adminService.getNormalUserList(page, pageSize, query, status, adminId);
    }

    // 获取商家列表
    @AdminRequired
    @GetMapping("/merchants")
    public Result<?> getMerchantList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String query,
            @RequestParam(required = false) Integer status,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("获取商家列表: page={}, pageSize={}, query={}, status={}", 
                page, pageSize, query, status);
        
        return adminService.getMerchantList(page, pageSize, query, status, adminId);
    }

    // 获取单个用户详情
    @AdminRequired
    @GetMapping("/users/{userId}")
    public Result<?> getUserDetail(
            @PathVariable Long userId,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("获取用户详情: userId={}", userId);
        return adminService.getUserDetail(userId, adminId);
    }

    // 更新用户信息
    @AdminRequired
    @PutMapping("/users/{userId}")
    public Result<?> updateUser(
            @PathVariable Long userId,
            @RequestBody UpdateUserRequest request,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("更新用户: userId={}, request={}", userId, request);
        return adminService.updateUser(userId, request, adminId);
    }

    // 删除用户
    @AdminRequired
    @DeleteMapping("/users/{userId}")
    public Result<?> deleteUser(
            @PathVariable Long userId,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("删除用户: userId={}", userId);
        return adminService.deleteUser(userId, adminId);
    }

    // 更新用户状态
    @AdminRequired
    @PutMapping("/users/{userId}/status")
    public Result<?> updateUserStatus(
            @PathVariable Long userId,
            @RequestParam Integer status,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("更新用户状态: userId={}, status={}", userId, status);
        return adminService.updateUserStatus(userId, status, adminId);
    }

    // 更新用户角色
    @AdminRequired
    @PutMapping("/users/{userId}/role")
    public Result<?> updateUserRole(
            @PathVariable Long userId,
            @RequestParam Long roleId,
            @RequestAttribute("userId") Long adminId) {
        
        log.info("更新用户角色: userId={}, roleId={}", userId, roleId);
        return adminService.updateUserRole(userId, roleId, adminId);
    }

    @PutMapping("/password")
    public Result<?> updatePassword(
            @RequestBody UpdatePasswordRequest request,
            @RequestAttribute("userId") Long userId) {
        
        log.info("修改密码: userId={}", userId);
        return adminService.updatePassword(userId, request);
    }

    @GetMapping("/profile")
    public Result<?> getProfile(@RequestAttribute("userId") Long userId) {
        log.info("获取个人信息: userId={}", userId);
        return adminService.getProfile(userId);
    }

    @PutMapping("/profile")
    public Result<?> updateProfile(
            @RequestBody UpdateProfileRequest request,
            @RequestAttribute("userId") Long userId) {
        
        log.info("更新个人信息: userId={}, request={}", userId, request);
        return adminService.updateProfile(userId, request);
    }
} 